package com.ryder.subject.application.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Preconditions;
import com.ryder.subject.application.convert.SubjectAnswerDTOConvert;
import com.ryder.subject.application.convert.SubjectInfoDTOConvert;
import com.ryder.subject.application.convert.SubjectLikedDTOConverter;
import com.ryder.subject.application.dto.SubjectInfoDTO;
import com.ryder.subject.application.dto.SubjectLikedDTO;
import com.ryder.subject.common.entity.PageResult;
import com.ryder.subject.common.entity.Result;
import com.ryder.subject.domain.entity.SubjectAnswerBO;
import com.ryder.subject.domain.entity.SubjectInfoBO;
import com.ryder.subject.domain.entity.SubjectLikedBO;
import com.ryder.subject.domain.service.SubjectInfoDomainService;
import com.ryder.subject.domain.service.SubjectLikedDomainService;
import com.ryder.subject.infra.basic.entity.SubjectInfoEs;
import com.ryder.subject.infra.basic.entity.SubjectLiked;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述：题目controller
 *
 * @author Ryder
 * @version 1.0
 * @date 2025/02/03
 * @since 1.0 // 版本号，指明从哪个版本开始
 */
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     *
     * @return Result true：成功 false：失败
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkArgument(StringUtils.isNotEmpty(subjectInfoDTO.getSubjectName()),
                    "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");

            Preconditions.checkArgument(CollectionUtils.isNotEmpty(subjectInfoDTO.getCategoryIds()), "题目分类不能为空");
            Preconditions.checkArgument(CollectionUtils.isNotEmpty(subjectInfoDTO.getCategoryIds()), "题目标签不能为空");

            //转bo
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.convertDtoToBo(subjectInfoDTO);
            //题目选项转bo
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConvert.INSTANCE
                    .convertDtoToBoList(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);

            subjectInfoDomainService.add(subjectInfoBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增标签失败");
        }
    }

    /**
     * 查询题目列表
     *
     * @return Result true：成功 false：失败
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> querySubjectList(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //校验
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            //转bo
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.convertDtoToBo(subjectInfoDTO);
            PageResult<SubjectInfoBO> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);

            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.fail("分页查询题目列表失败");
        }
    }

    /**
     * 查询题目详情
     *
     * @return Result true：成功 false：失败
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //校验
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            //转bo
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.convertDtoToBo(subjectInfoDTO);
            SubjectInfoBO boPageResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO resultDTO = SubjectInfoDTOConvert.INSTANCE.convertBoToDto(boPageResult);
            log.info("SubjectController.querySubjectInfo.resultDTO:{}", JSON.toJSONString(resultDTO));
            return Result.ok(resultDTO);
        } catch (Exception e) {
            log.error("SubjectController.querySubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

    /**
     * 全文关键词高亮检索
     *
     * @return PageResult<SubjectInfoEs> subjectInfoEs集合的分页结果
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //校验
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWord()), "关键词不能为空");
            //转bo
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.convertDtoToBo(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoEs> boPageResult = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);

            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文关键词高亮检索失败");
        }
    }

    /**
     * 获取题目的贡献榜
     *
     * @return List<SubjectInfoDTO> 排行榜前几名
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {
        try {
            List<SubjectInfoBO> boList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> dtoList = SubjectInfoDTOConvert.INSTANCE.convertBoToDtoList(boList);
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取贡献榜失败");
        }
    }
}
